package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.user.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/courses")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/subject/{subject}")
    public Result getBySubject(@PathVariable Integer subject){
        List<Course> courses =  courseService.getBySubject(subject);
        return Result.success(courses);
    }


    @GetMapping("/list")
    public Result getAll(){
        List<Course> courses = courseService.getAll();
        return Result.success(courses);
    }

    @PutMapping
    public Result update(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
         Course course = courseService.getById(id);
         return Result.success(course);
    }

    @PostMapping
    public Result add(@RequestBody Course course){
        courseService.add(course);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        courseService.deleteById(id);
        return Result.success();
    }

    @GetMapping
    public Result list(CourseDTO dto){
        PageResult<Course> pageResult = courseService.list(dto);
        return Result.success(pageResult);
    }

}
